<?php
namespace app\cdyoa\controller;
use think\Db;
class Log extends Base
{   
  /**
     * 点位列表
     */
    public function lists(){
        //ajax查询数据
        if( is_post() ){
            $page     = input('page');
            $type     = input('type');
            $keywords = input('keywords');
            $min_time = input('min_time');
            $max_time = input('max_time');
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    log l
                left join
                    admin_user au
                on
                    l.action_user_id = au.id
                where
                    1 = ?
                ";
            $param = [1];

            if ( $type != '100all' ) { //全部
                $sql    .= " and l.type = ?";
                $param[] = $type;
            }

            if ( !empty($keywords) ) {
                $sql    .= " and l.msg like ?";
                $param[] = "%{$keywords}%";
            }
            if ( !empty($min_time) ) {
                $sql    .= " and l.create_time >= ?";
                $param[] = strtotime($min_time);
            }
            if ( !empty($max_time) ) {
                $sql    .= " and l.create_time <= ?";
                $param[] = strtotime($max_time);
            }

            //总条数
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            
            //分页数据
            $page_size   = c('page_size');     #分页条数
            $page        = com_cur_page($total, $page,  $page_size);   #当前页
            $page_data   = com_page_data($total, $page,  $page_size);   #分页原始数据
            
            //列表数据
            $start       = ($page - 1) * $page_size;
            $sql        .= " order by l.create_time desc,l.id desc limit $start, $page_size";
            $field       = "l.id,l.user_id,l.action_user_id,l.msg,l.create_time,l.type,au.username";
            $sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){
                $list[$k]['create_time']  =  com_format_time( $v['create_time'] );

                $list[$k]['type']         =  c('type.log_type',$v['type']);
            }
            $com_body    = com_build_table($list, array( "id","type", "msg","create_time","username"));
            $com_page    = com_build_page( $page_data );
            $data = array(
                "com_body"  => $com_body,
                "com_page"  => $com_page,
            );
            
            $config = [
                'data'   => $data,
                'suc'    => [
                    'js_action' => 'function',
                ],
            ];
            com_check($config);
        }
        $data = [
            'nav'       => ['日志列表', '列表'],
            'type'      => ['100all'=>'全部类型'] + c('type.log_type'),
        ];
        return view('', $data);
    }

}